package com.hex.liyue.launchdatemodule.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.picker.TimePicker;
import com.hex.liyue.giftmodule.activity.MyGiftsStoresActivity;
import com.hex.liyue.launchdatemodule.presenter.ILaunchDataView;
import com.hex.liyue.launchdatemodule.presenter.LaunchDataPresenter;
import com.hex.liyue.usermodule.view.DatePicker;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.io.File;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这是一个发起约会界面
 * Created by WJ on 2016/6/7.
 */
public class LaunchDateActivity extends BaseActivity implements ILaunchDataView {

    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;

    @Bind(R.id.tv_location)
    EditText tvLocation;
    @Bind(R.id.tv_invitations_time)
    TextView tvInvitationsTime;
    @Bind(R.id.et_remark)
    EditText etRemark;
    @Bind(R.id.rl_invitations_time)
    RelativeLayout rlInvitationsTime;
    @Bind(R.id.rl_location)
    RelativeLayout rlLocation;
    @Bind(R.id.ll_make_video)
    LinearLayout llMakeVideo;
    @Bind(R.id.tv_make_video)
    TextView tvMakeVideo;
    @Bind(R.id.iv_video_icon)
    ImageView iv_video_icon;
    @Bind(R.id.iv_want_gift)
    ImageView iv_want_gift;
    @Bind(R.id.tv_gift)
    TextView tv_gift;
    @Bind(R.id.tv_want_things)
    TextView etEvent;

    public static String location;
    public static String latitude;
    public static String longitude;
    private LaunchDataPresenter launchDataPresenter;
    private String time;//选中的时间
    private final int MAKE_VIDEO_REQUEST = 1;
    private final int LOCATION_REQUEST = 2;
    private final int DEMAND_REQUEST = 3;
    private final int WANT_REQUEST = 4;
    private String videoPath = null;
    public static String mGiftId, gift_id, event;
    private String flag = "";
    private String appID = "wxbd7b91a516360a2a";
    private String appSecret = "8c51d672ccdc0d6933ef875a4a5b6a2d";
    private IWXAPI wxApi;

    @Override
    protected void bindPresenter() {
        launchDataPresenter = new LaunchDataPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        wxApi = WXAPIFactory.createWXAPI(this, "wxbd7b91a516360a2a");
        wxApi.registerApp("wxbd7b91a516360a2a");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_launch_date;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        tvToobarTitle.setText("发起约会");
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.VISIBLE);
        tvToobarRight.setText("完成");
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public String getEvent() {
        return etEvent.getText().toString();
    }

    @Override
    public String getLocation() {
        return tvLocation.getText().toString();
    }

    @Override
    public String getInvitationsTime() {
        return tvInvitationsTime.getText().toString();
    }

    @Override
    public File getVideo() {
        if (videoPath == null) {
            //没视频
            return null;
        } else {
            return new File(videoPath);
        }
    }

    @Override
    public String getRemark() {
        return etRemark.getText().toString();
    }

    @Override
    public void startLocationActivity() {
        Intent intent = new Intent(this, LocationActivity.class);
        intent.putExtra("tvLocation", getLocation());
        startActivityForResult(intent, LOCATION_REQUEST);
    }

    @Override
    public void startRecorderVideoActivity() {
        Intent intent = new Intent(this, RecorderVideoActivity.class);
        startActivityForResult(intent, MAKE_VIDEO_REQUEST);
    }

    @Override
    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("拍摄一段视频，会大大提高您的约会几率哟!(别问大大是谁)");
        builder.setPositiveButton("去拍视频", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startRecorderVideoActivity();
            }
        });
        builder.setNegativeButton("不必拍了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if ("".equals(flag)) {
//                    shareWeiXin();
                    if (getLocation().equals(location)) {
                        launchDataPresenter.upload(null, "", longitude, latitude);
                    } else {
                        launchDataPresenter.upload(null, "", "", "");
                    }
                } else {
                    if (getLocation().equals(location)) {
                        launchDataPresenter.upload(null, mGiftId, longitude, latitude);
                    } else {
                        launchDataPresenter.upload(null, mGiftId, "", "");
                    }
                }
            }
        });
        builder.show();

    }

    @Override
    public void startLaunchListActivity() {
        Intent intent = new Intent(this, LaunchListActivity.class);
        intent.putExtra("falg", "flag");
        intent.putExtra("isShare", true);
        intent.putExtra("event", getEvent());
        startActivity(intent);
        finish();
    }

    @Override
    public void startMyGiftsStoresActivity() {
        Intent intent = new Intent(this, MyGiftsStoresActivity.class);
        startActivityForResult(intent, DEMAND_REQUEST);
    }

    @Override
    public void startSelectWantActivity() {
        Intent intent = new Intent(this, SelectLaunchThingsActivity.class);
        startActivityForResult(intent, WANT_REQUEST);
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right, R.id.rl_invitations_time, R.id.iv_location, R.id.ll_make_video, R.id.ll_demand_gift, R.id.rl_wang_things})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right:
                if ("".equals(flag)) {
                    if (getLocation().equals(location)) {
                        launchDataPresenter.finish("", longitude, latitude);
                    } else {
                        launchDataPresenter.finish("", "", "");
                    }
                } else {
                    if (getLocation().equals(location)) {
                        launchDataPresenter.finish(mGiftId, longitude, latitude);
                    } else {
                        launchDataPresenter.finish(mGiftId, "", "");
                    }
                }
                break;
            case R.id.rl_invitations_time:
                showInvitationsTime();
                break;
            case R.id.iv_location:
                startLocationActivity();
                break;
            case R.id.ll_make_video:
                startRecorderVideoActivity();
                break;
            case R.id.ll_demand_gift:
                startMyGiftsStoresActivity();
                break;
            case R.id.rl_wang_things://选择想要的做的事
                startSelectWantActivity();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MAKE_VIDEO_REQUEST && resultCode == RESULT_OK) {
            L.d("WJ", "录完视频回来了" + data.getStringExtra("path"));
            videoPath = data.getStringExtra("path");
            llMakeVideo.setEnabled(false);
            tvMakeVideo.setText("视频拍好了");
            Glide.with(this).load(R.drawable.success_icon).into(iv_video_icon);
        } else if (requestCode == LOCATION_REQUEST && resultCode == RESULT_OK) {
            L.d("WJ", "选了地址");
            L.d("WJ", latitude);
            L.d("WJ", longitude);
            tvLocation.setText(location);
        } else if (requestCode == DEMAND_REQUEST && resultCode == RESULT_OK) {//选择想要礼物
            Glide.with(this).load(R.drawable.success_icon).into(iv_want_gift);
            tv_gift.setText("礼物选好啦");
            flag = "flag";
            L.d("WJ", mGiftId);
        } else if (requestCode == WANT_REQUEST && resultCode == RESULT_OK) {//选择想要做的事
            etEvent.setText(event);
        }
    }

    /**
     * 显示时间对话框
     */
    private void showInvitationsTime() {
        final Calendar c = Calendar.getInstance();
        DatePicker datePicker = new DatePicker(this);
        int NowYear = DateUtils.getNowYear();
        datePicker.setRange(NowYear, NowYear + 3);//年份范围
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                time = year + "-" + month + "-" + day;
                TimePicker timePicker = new TimePicker(LaunchDateActivity.this, TimePicker.Mode.HOUR_OF_DAY);
                String NowYear = DateUtils.getFormatDate(System.currentTimeMillis(), "HH:mm");
                timePicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
                    @Override
                    public void onTimePicked(String hour, String minute) {
                        time += " " + hour + ":" + minute;
                        tvInvitationsTime.setText(time);
                    }
                });
                timePicker.show();
            }
        });
        datePicker.show();
    }

}
